/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package org.apache.cordova;

import java.util.Iterator;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;


/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Title <B>todo</B>
 * <pre>todo</pre>
 */

public class CordovaWebPlugin extends CordovaPlugin{
    private static String TAG = "CordovaWebPlugin";

    @Override
    public Boolean shouldAllowRequest(String url) {
        return true;
    }
    @Override
    public Boolean shouldAllowNavigation(String url) {
    	return true;
    }
    
    @Override
    public boolean onOverrideUrlLoading(String url) {
    	return false;
    }
    
    @Override
    public Boolean shouldOpenExternalUrl(String url) {
    	return true;
    }
    
    String calls = "";
    @Override
    public void onStart() {
        calls += "start,";
        LOG.d(TAG, "onStart");
    }
    @Override
    public void onPause(boolean multitasking) {
        calls += "pause,";
        LOG.d(TAG, "onPause");
    }
    @Override
    public void onResume(boolean multitasking) {
        calls += "resume,";
        LOG.d(TAG, "onResume");
    }
    @Override
    public void onStop() {
        calls += "stop,";
        LOG.d(TAG, "onStop");
    }
    
    public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
        /*if (action.equals("start")) {
            String className = args.isNull(0) ? MainTestActivity.class.getCanonicalName() : args.getString(0);
            String startUrl = args.getString(1);
            JSONObject extraPrefs = args.getJSONObject(2);
            this.startActivity(className, startUrl, extraPrefs);
            callbackContext.success();
            return true;
        }*/
        return false;
    }

    public void startActivity(String className, String startUrl, JSONObject extraPrefs) throws JSONException {
        try {
            if (!startUrl.contains(":")) {
                startUrl = "file:///android_asset/www/" + startUrl;
            }
            Intent intent = new Intent(this.cordova.getActivity(), Class.forName(className));
            intent.putExtra("testStartUrl", startUrl);
            Iterator<String> iter = extraPrefs.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                intent.putExtra(key, extraPrefs.getString(key));
            }
            LOG.d(TAG, "Starting activity %s", className);
            this.cordova.getActivity().startActivity(intent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            LOG.e(TAG, "Error starting activity %s", className);
        }
    }
}
